define(['jquery', 'bootstrap', 'backend', 'table', 'form', '/assets/addons/kuerp/layui/layui.js'], function ($, undefined, Backend, Table, Form, Layui) {

    var Controller = {
        index: function () {

            layui.use(['table','upload','form'], function(){
                var table = layui.table;
                upload = layui.upload;

                var set = Config.set;
                var examine = parseInt(set.examine.is_checked);
                if(isNaN(examine)){
                    examine = 0;
                }
                if(examine!=1){
                    hideStatus = true;
                }else{
                    hideStatus = false;
                }
                var taxes = parseInt(set.taxes.is_checked);
                if(isNaN(taxes)){
                    taxes = 0;
                }
                if(taxes!=1){
                    taxesStatus = true;
                }else{
                    taxesStatus = false;
                }
                var price_decimal = parseInt(set.price_decimal.value);
                if(isNaN(price_decimal)){
                    price_decimal = 1;
                }
                var number_decimal = parseInt(set.number_decimal.value);
                if(isNaN(number_decimal)){
                    number_decimal = 1;
                }

                table.render({
                    elem: '#table'
                    ,url:'kuerp/goods_batch_log/index'
                    ,parseData: function(res){ //res 即为原始返回的数据
                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": res.total, //解析数据长度
                            "data": res.rows //解析数据列表
                        };
                    }
                    ,toolbar: '#thisToolbar' //开启头部工具栏，并为其绑定左侧模板
                    ,defaultToolbar: [
                        {name:'filter', title: __('Filter')}, // 列筛选
                         {
                            name: 'exports',
                            title: __('Exports'),
                            onClick: function(obj) {
                              // 获得数据并清除临时字段
                              var data = table.clearCacheKey(obj.data);
                              // 当前示例配置项
                              var options = obj.config;

                              if(data.length > 0 || Object.keys(data).length > 0){
                                table.exportFile(options.id, null, 'csv');
                              }else{
                                layer.msg(__('No data'));
                              }
                             
                            }
                          },
                        {name:'print', title: __('Print')} // 打印
                    ]
                    ,cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    ,text: {
                        none: __('No data'),
                        loading: __('Loading...'),
                    }
                    ,where: {
                        filter:JSON.stringify(getSearch()),
                        op:JSON.stringify(getOp()),
                    }
                    ,cols: [[
                        {field: 'goods_code', title: __('Goods_code'),templet: function (d) {
                                if(d.goods.goods_code!=null){
                                    return d.goods.goods_code;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'goods_name', title: __('Goods_name'),templet: function (d) {
                                if(d.goods.goods_name!=null){
                                    return d.goods.goods_name;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'unit_price', title: __('Unit_price'),templet: function (d) {
                                return (d.unit_price).toFixed(price_decimal);
                            }},
                        {field: 'tax_included', title: __('Tax_included'),templet: function (d) {
                                return (d.tax_included).toFixed(price_decimal);
                            },hide:taxesStatus},
                        {field: 'all_money', title: __('All_money'),templet: function (d) {
                                return (d.all_money).toFixed(price_decimal);
                            }},
                        {field: 'tax_money', title: __('Tax_money'),templet: function (d) {
                                return (d.tax_money).toFixed(price_decimal);
                            },hide:taxesStatus},
                        {field: 'money_tax_price', title: __('Money_tax_price'),templet: function (d) {
                                return (d.money_tax_price).toFixed(price_decimal);
                            },hide:taxesStatus},

                        {field: 'address', title: __('place of origin'),templet: function (d) {
                                if(d.batch.address!=null){
                                    return d.batch.address;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'registration', title: __('Registration_number'),templet: function (d) {
                                if(d.batch.registration!=null){
                                    return d.batch.registration;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'production', title: __('Production_license'),templet: function (d) {
                                if(d.batch.production!=null){
                                    return d.batch.production;
                                }else{
                                    return '';
                                }
                            }},

                        {field: 'goods_unit', title: __('Goods_unit'),templet: function (d) {
                                if(d.unit.name!=null){
                                    return d.unit.name;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'unitconversion', title: __('Sub_unit'),templet: function (d) {
                                if(d.unitconversion!=null){
                                    return d.unitconversion;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'stores_id', title: __('Warehouse'),templet: function (d) {
                                if(d.stores.name!=null){
                                    return d.stores.name;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'batch_name', title: __('Batch'),templet: function (d) {
                                if(d.batch.batch_name!=null){
                                    return d.batch.batch_name;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'manufacture', title: __('Production_date'),templet: function (d) {
                                return layui.util.toDateString(d.batch.manufacture*1000,"yyyy-MM-dd");
                            },width:110},
                        {field: 'guarantee', title: __('Shelf_life'),templet: function (d) {
                                if(d.batch.guarantee!=null){
                                    return d.batch.guarantee;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'validity', title: __('Validity'),templet: function (d) {
                                return layui.util.toDateString(d.batch.validity*1000,"yyyy-MM-dd");
                            },width:110},
                        {field: 'more_unit_num', title: __('Number'),templet: function (d) {
                                if(d.more_unit_num!=null){
                                    return d.more_unit_num;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'unit_name', title: __('Sub_unit_count'),templet: function (d) {
                                if(d.number!=null){
                                    return d.sub_unit;
                                }else{
                                    return '';
                                }
                            }},

                        {field: 'purchase_code', title: __('Purchase_code'),width:150},
                        {field: 'purchase_time', title: __('Purchase_time'),templet: function (d) {
                                return layui.util.toDateString(d.purchase_time*1000,"yyyy-MM-dd");
                            },width:120},
                        {field: 'business_type', title: __('Business_type'),templet: function (d) {
                                if(d.business_type==1){
                                    return __('Business_type 1');
                                }
                                if(d.business_type==2){
                                    return __('Business_type 2');
                                }
                                if(d.business_type==3){
                                    return __('Business_type 3');
                                }
                                if(d.business_type==4){
                                    return __('Business_type 4');
                                }
                                if(d.business_type==5){
                                    return __('Business_type 5');
                                }
                                if(d.business_type==6){
                                    return __('Business_type 6');
                                }
                                if(d.business_type==7){
                                    return __('Business_type 7');
                                }
                                if(d.business_type==8){
                                    return __('Business_type 8');
                                }
                                if(d.business_type==9){
                                    return __('Business_type 9');
                                }
                                if(d.business_type==10){
                                    return __('Business_type 10');
                                }
                                if(d.business_type==11){
                                    return __('Assembly_order');
                                }
                                if(d.business_type==12){
                                    return __('Assembly_order');
                                }
                                if(d.business_type==13){
                                    return __('Disassembly_order');
                                }
                                if(d.business_type==14){
                                    return __('Disassembly_order');
                                }
                                if(d.business_type==15){
                                    return __('Outsourcing');
                                }
                                if(d.business_type==16){
                                    return __('Acceptance');
                                }
                            }},
                        {field: 'customer_id', title: __('Customer_id'),templet: function (d) {
                                if(d.customer_id!=null){
                                    return d.customer_id;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'sale_id', title: __('Sale_id')},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE'},
                    ]]
                    ,done: function(res, curr, count){
                        if (res.count == 0 && !searchStatus) {
                            var html = '<div class="layui-none no-results"><span>' + __('Please enter search criteria and click search') + '</span></div>';
                            $(".layui-table-main").html(html);
                        }
                    }
                });

                upload.render({
                    elem: '#import'
                    ,url: 'ajax/upload'
                    ,done: function(res, index, upload){ //上传后的回调
                        if(res.code==1){
                            Fast.api.ajax("kuerp/goods_batch_log/import?file="+res.data.url, "",'',true);
                        }else{
                            Toastr.error(res.msg);
                        }
                    }
                    ,accept: 'file' //允许上传的文件类型
                    ,size: 10240 //最大允许上传的文件大小
                });

                layui.form.on('switch(switchList)', function (obj) {
                    var onoff = this.checked ? '1' : '0';
                    var id = $(this).data('id');
                    $.ajax( {
                        url: "kuerp/goods_batch_log/multi",
                        type: "POST",
                        data:{
                            'ids':id,
                            'params':"status="+onoff,
                        },
                        success: function(res){
                            if(res.code==0){
                                Toastr.error(res.msg);
                            }else{
                                Toastr.success(__('Operation_successful'));
                            }
                        }
                    });
                });

                //头工具栏事件
                table.on('toolbar(table)', function(obj){
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch(obj.event){
                        case 'refresh':
                            table.reload('table', []);
                            break;
                        case 'add':
                            Fast.api.open("kuerp/goods_batch_log/add", __("Add"), {
                                callback:function(value){
                                    Controller.api.bindevent();
                                }
                            });
                            break;
                        case 'delAll':
                            var data = checkStatus.data;
                            var ids = '';
                            $(data).each(function (k,v) {
                                ids+=v.id+',';
                            });
                            Fast.api.ajax("kuerp/goods_batch_log/del?ids="+ids, function () {
                                reloadTable()
                            },'',true);
                            break;
                    }
                });
                function reloadTable(){
                    table.reload('table', {
                        where: {
                            filter:JSON.stringify(getSearch()),
                            op:JSON.stringify(getOp()),
                        }
                        ,page: {
                            curr: 1
                        }
                    });
                }
                //监听行工具事件
                table.on('tool(table)', function(obj){
                    var data = obj.data;
                    if(obj.event === 'del'){
                        $.ajax( {
                            url: "kuerp/goods_batch_log/del",
                            type: "POST",
                            data:{'ids':data.id},
                            success: function(res){
                                if(res.code==1){
                                    Toastr.success(__('Operation_successful'));
                                    setTimeout(function () {
                                        reloadTable();
                                    },1000)
                                }else{
                                    Toastr.error(res.msg);
                                }
                            }
                        });
                    } else if(obj.event === 'edit'){
                        Fast.api.open("kuerp/goods_batch_log/edit?ids="+data.id, __("Edit"));
                    }
                });
                //搜索事件
                $('#search').on('click', function () {
                    table.reload('table', {
                        where: {
                            filter:JSON.stringify(getSearch()),
                            op:JSON.stringify(getOp()),
                        }
                        ,page: {
                            curr: 1,
                            prev: __('Previous'),
                            next: __('Next'),
                            first: __('First'),
                            last: __('Last'),
                            skipText: [__('Go to page'), '', __('Confirm')],
                            countText: [__('Total') + ' ',' ' +  __('Records')],
                            limitTemplet: function(item) {
                                return item + ' ' + __('entries');
                            }
                        }
                    });
                });
                var searchStatus = false;
                function getSearch() {
                    var search = {};
                    var goods_name = $('#goods_name').val();
                    if(goods_name!=''){
                        var search = {'goods.goods_name':goods_name};
                    }
                    var batch_name = $('#batch_name').val();
                    if(batch_name!=''){
                        if(goods_name!=''){
                            var search = {'goods.goods_name':goods_name,'batch.batch_name':batch_name};
                        }else{
                            var search = {'batch.batch_name':batch_name};
                        }
                    }
                    var purchase_code = $('#purchase_code').val();
                    if(purchase_code!=''){
                        search.purchase_code = purchase_code;
                    }
                    var purchase_time = $('#purchase_time').val();
                    if(purchase_time!=''){
                        search.purchase_time = purchase_time;
                    }
                    var business_type = $('#business_type').val();
                    if(business_type!=''){
                        search.business_type = business_type;
                    }
                    var sale_id = $('#sale_id').val();
                    if(sale_id!=''){
                        search.sale_id = sale_id;
                    }
                    if(goods_name=='' && batch_name=='' && purchase_code=='' && purchase_time=='' && business_type=='' && sale_id==''){
                        searchStatus = false;
                        search.id = 1;
                    }else{
                        searchStatus = true;
                    }
                    return search;
                }
                function getOp() {
                    var op = {'goods.goods_name':'like','batch.batch_name':'like','purchase_code':'like','purchase_time':'BETWEEN','business_type':'=','sale_id':'='};
                    return op;
                }
            });
            Controller.api.bindevent();
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                layui.use('laydate', function(){
                    var laydate = layui.laydate;
                    //执行一个laydate实例

                    laydate.render({
                         lang: Config.lang == 'zh-cn' ? 'cn' : 'en',
                        elem: '#c-purchase_time',
                        type: 'datetime'
                    });
                    laydate.render({
                         lang: Config.lang == 'zh-cn' ? 'cn' : 'en',
                        elem: '#purchase_time',
                        type: 'datetime',
                        range: true
                    });
                });
                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    parent.location.reload();
                });
            }
        }
    };
    return Controller;
});